<template>
  <div class="login">
    <div class="bg"></div>
    <div class="content">
      <h1>登录</h1>
      <van-form @submit="onSubmit">
        <van-field
          clickable
          v-model="hospitalInfo.mobile"
          left-icon="phone"
          placeholder="请输入手机号"
          @blur="regmobile"
          @focus="getFocus('mobile')"
          :rules="[{ required: true, message: '' }]"
        />
        <van-field
          v-model="hospitalInfo.code"
          center
          clearable
          left-icon="lock"
          @focus="getFocus('code')"
          placeholder="请输入短信验证码"
        >
          <template #button>
            <van-button
              @click.prevent="getcode"
              size="small"
              type="primary"
              :disabled="!isShow"
              >{{ btntxt }}
            </van-button>
          </template>
        </van-field>
        <a href="#">忘记密码?</a>
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit"
            >登录</van-button
          >
        </div>
      </van-form>
      <van-number-keyboard
        :show="show"
        :maxlength="11"
        @blur="show = false"
        @input="onInput"
        @delete="onDelete"
        @touchstart.native.stop="show = true"
      />
    </div>
    <div class="other-login">
      <p><span>—— 使用第三方帐号登陆 ——</span></p>
      <div class="third">
        <ul>
          <li><img src="../../assets/icon-qq.png" /></li>
          <li><img src="../../assets/icon-weibo.png" /></li>
          <li><img src="../../assets/icon-weixin.png" /></li>
        </ul>
      </div>
    </div>
    <p>
      登录使用就表示同意用户协议<a href="javascript:;" style="color:#357bd4"
        >条款</a
      >和<a href="javascript:;" style="color:#357bd4">隐私</a>政策
    </p>
  </div>
</template>

<script>
import { userLogin } from '../../api/login'
import { saveToken } from '../../utils/token'
export default {
  name: 'login',
  data () {
    return {
      hospitalInfo: {
        mobile: '15878456521',
        code: '246810'
      },
      diffType: 'mobile',
      isShow: true,
      time: 6,
      show: false,
      value: '',
      btntxt: '发送验证码',
      ceshi: ''
    }
  },
  methods: {
    // 数字键盘输入
    onInput (value) {
      this.hospitalInfo[this.diffType] = `${
        this.hospitalInfo[this.diffType]
      }${value}`
    },
    // --------------------------------------键盘删除事件----------------------
    onDelete () {
      const value = this.hospitalInfo[this.diffType].toString()
      if (Number(value.slice(0, value.length - 1)) === 0) {
        this.hospitalInfo[this.diffType] = ''
      } else {
        this.hospitalInfo[this.diffType] = Number(
          value.slice(0, value.length - 1)
        )
      }
    },
    // -----------------------------获得焦点------------------------------
    getFocus (str) {
      this.diffType = str
      this.show = true
    },
    // -----------------------------自定义手机号校验 ------------------------
    regmobile () {
      const regmobile = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
      if (!regmobile.test(this.hospitalInfo.mobile)) {
        this.$toast('手机号无效')
        return false
      }
      return true
    },

    // --------------------------------获取验证码-----------------------
    getcode () {
      if (this.regmobile() === false) {
        this.$toast('手机号无效')
        return false
      }

      this.$toast({
        message: '已发送',
        onClose: () => {
          this.time = 5
          this.timer()
          this.isShow = false
        }
      })
    },
    // ------------------------------发送手机验证码倒计时-------------------
    timer () {
      if (this.time > 0) {
        this.disabled = true
        this.time--
        this.btntxt = this.time + '秒'
        setTimeout(this.timer, 1000)
      } else {
        this.time = 0
        this.btntxt = '发送验证码'
        this.isShow = true
      }
    },
    // ----------------------------------倒计时结束------------------------
    finish () {
      this.isShow = true
      this.disableds = false
    },
    // -----------------------------------提交按钮-------------------------
    onSubmit () {
      if (this.hospitalInfo.code === '' || this.hospitalInfo.code === null) {
        this.$toast('请输入验证码')
        return false
      }
      if (this.hospitalInfo.code !== '246810') {
        this.$toast('验证码错误')
        this.hospitalInfo.code = ''
        return false
      }

      this.regmobile()
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true
      })
      userLogin({
        mobile: this.hospitalInfo.mobile,
        code: this.hospitalInfo.code
      }).then(res => {
        if (res.status === 201) {
          saveToken(res.data.data)
          this.$toast.success('登录成功')
          if (this.$route.query.back) {
            // 原路返回
            return this.$router.push({ path: this.$route.query.back })
          }
          this.$router.push({ path: '/home' })
        }
      })
    }
  }
}
</script>

<style lang="less" scope>
// 提交按钮
.van-button--info {
  background-image: linear-gradient(to right, #7bbecb, #537cb3, #4259a7);
  border: none !important;
  width: 90% !important;
  margin: 0 auto !important;
}
// 文本框下面的线
.van-cell::after {
  border-bottom: 1px solid #9c9a9a !important;
}

// 发送验证码按钮
.van-button--primary {
  width: 90px;
  color: #6093bb !important;
  border: 1px solid #6093bb !important;
  background-color: #fff !important ;
  border-radius: 8px !important;
}

// 图标
.van-field__left-icon .van-icon,
.van-field__right-icon .van-icon,
.code {
  color: #6093bb !important;
}
.van-field__left-icon {
  margin-right: 10px !important;
}
// 登录
.content {
  position: relative;
  width: 88%;
  height: 220px;
  margin: 0 auto;
  padding: 0 10px;
  top: -20px;
  background-color: #fff;
  border-radius: 7px;
  box-shadow: 2px 2px 10px #74b2c7;
}
.content h1 {
  color: #4667aa;
  padding-left: 15px;
  font-size: 22px;
  font-weight: 500;
  line-height: 60px;
}
.content a {
  float: right;
  padding: 10px;
  padding-right: 15px;
  color: #6093bb;
  font-size: 12px;
}
.login {
  background-color: #edf3f9;
  height: 100vh;
  // 其它方式登录
  .other-login {
    font-size: 12px;
    p {
      text-align: center;
      width: 4.7rem;
      margin: 0 auto;
      color: black;
    }
    .third ul {
      display: flex;
      align-items: center;
      justify-content: center;
      margin-top: 20px;
      li {
        width: 64px;
        height: 36px;
        text-align: center;
        line-height: 36px;
      }
      img {
        height: 100%;
      }
    }
  }

  //同意协议
  p {
    font-size: 0.2rem;
    color: black;
    text-align: center;
    padding: 0.1rem 0 0.4rem 0;
    letter-spacing: 0.01rem;
    padding-top: 20px;
  }
}
// 背景图
.bg {
  height: 210px;
  background: url('../../assets/bg4.jpg') no-repeat;
  background-size: 100% 100%;
  opacity: 0.8;
}
.van-nav-bar {
  background-color: transparent !important;
  height: 100% !important;
}
.van-nav-bar__left {
  padding: 0 !important;
}
.van-nav-bar__text,
.van-nav-bar .van-icon {
  color: #fff !important;
  font-size: 20px;
}
</style>
